import time
import context

const short_duration = 2 * time.millisecond // a reasonable duration to block in an example

// This example passes a context with a timeout to tell a blocking function that
// it should abandon its work after the timeout elapses.
fn main() {
    // Pass a context with a timeout to tell a blocking function that it
    // should abandon its work after the timeout elapses.
    mut background := context.background()
    mut ctx, cancel := context.with_timeout(mut &background, short_duration)
    defer {
        cancel()
        eprintln('> defer finishes')
    }

    ctx_ch := ctx.done()
    select {
        _ := <-ctx_ch {
            eprintln('> reading from ctx_ch succeeded')
        }
        1 * time.second {
            panic('This should not happen')
        }
    }
    eprintln('> main finishes')
}
